package com.qingfeng.client.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qingfeng.client.DO.*;
import com.qingfeng.client.VO.*;
import com.qingfeng.client.bean.Buyer;
import com.qingfeng.client.bean.GoodsCommon;
import com.qingfeng.client.bean.OrderStore;
import com.qingfeng.client.bean.Trade;
import com.qingfeng.client.service.OrderService;
import com.qingfeng.client.util.Page;
import com.qingfeng.util.ApiMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.DelayQueue;

/**
 * 订单相关接口
 * @author zhangyuefei
 * @version 1.0
 * @date 2020/1/6 4:32 下午
 */
@Api(tags = "订单信息")
@RestController
@RequestMapping("/order")
public class OrderApi extends BaseV1Api {

    @Autowired
    private OrderService orderService;


    /**
     * 先预下单，后提交订单
     * @param orderGoodsVos
     * @return
     */
    @ApiOperation("预备订单,获取商品订单基本信息")
    @PostMapping("/preTrade")
    public ApiMsg<PreTradeDo> preTrade(@RequestBody List<OrderGoodsVo> orderGoodsVos) {
        Buyer buyer = getUser();
        if (CollectionUtils.isEmpty(orderGoodsVos)){
            return error();
        }
        PreTradeDo preTradeDo = orderService.preTrade(orderGoodsVos, buyer.getId());
        if (preTradeDo!=null){
            setPreTrade(preTradeDo);
            return success();
        }else {
            return error("失败");
        }

    }

    /**
     * 获取订单信息
     * @return
     */
    @ApiOperation("获取预备订单")
    @GetMapping("/myPreTrade")
    public ApiMsg<PreTradeDo> getMyPreTrade() {
        PreTradeDo preTradeDo = getPreTrade();
        return success(preTradeDo);
    }


    @ApiOperation("提交订单")
    @PostMapping("/createTrade")
    public ApiMsg createTrade(@RequestBody TradeVo tradeVo) {
        Buyer buyer = getUser();
        if (buyer == null) {
            return error("用户未登录");
        }
        tradeVo.setBuyerId(buyer.getId());
        Trade trade = orderService.createTrade(tradeVo);
        return success(trade);
    }

    @ApiOperation("确认收货")
    @PostMapping("/confirm/{orderStoreId}")
    public ApiMsg confirm(@PathVariable("orderStoreId")Long orderStoreId){
        orderService.confirm(orderStoreId);
        return success();
    }

    @ApiOperation("查询订单")
    @PostMapping("/queryOrder")
    public ApiMsg<IPage<OrderStoreDo>> query(@RequestBody OrderQueryVo orderQueryVo){

        Page page = orderQueryVo.getPage();
        OrderStoreCondition condition = orderQueryVo.getCondition();
        Buyer buyer = getUserByToken();
        condition.setBuyerId(buyer.getId());
        IPage<OrderStoreDo> page1 = orderService.query(page,condition);
        return successPage(page1);
    }

    @ApiOperation("查询订单详情")
    @GetMapping("/detail/{id}")
    public ApiMsg<OrderDetailDo> detail(@PathVariable("id")Long id){
        OrderDetailDo orderDetailDo = orderService.detail(id);
        return success(orderDetailDo);
    }

    @ApiOperation("计算多个商品运费")
    @PostMapping("/sumTransformFee")
    public ApiMsg getTransformFee(@RequestBody TransformFeeVo transformFeeVo){
        if (transformFeeVo.getAddressId()==null){
            return error("请先选择地址");
        }
        List<TransformFeeDo> list = orderService.getTransformFee(transformFeeVo);
        return success(list);
    }


}

